﻿using Coldairarrow.Entity.Base_leasesystem_accountrecharge;
using Coldairarrow.Entity.Base_leasesystem_bankcard;
using Coldairarrow.Entity.Base_leasesystem_merchant;
using Coldairarrow.Entity.Base_leasesystem_transactionbatch;
using Coldairarrow.Entity.Base_leasesystem_transactionbatchsub;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_accountrecharge
{
    public class leasesystem_accountrechargeBusiness : BaseBusiness<leasesystem_accountrecharge>, Ileasesystem_accountrechargeBusiness, ITransientDependency
    {
        public leasesystem_accountrechargeBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<leasesystem_accountrecharge>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_accountrecharge>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_accountrecharge, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<object> GetHeightSearch(PageInput<ConditionDTO> input)
        {
            var q = Db.GetIQueryable<leasesystem_accountrecharge>();
            var obj = q.Join(Db.GetIQueryable<leasesystem_merchant>(), a => a.MID, b => b.Id, (a, b) => new {
            
            a.Id,
            a.ServiceAccountNo,
            a.ServiceProviderID,
            a.ServiceProviderName,
            a.BusinessOrderNo,
            a.RechargeAmount,
            a.ruleType,
            a.RechargeStatus,
            a.MID,
            a.Order_no,
                b.customerName
            });
            var newWhere = obj;
            

            if (!input.Search.Keyword.IsNullOrEmpty()) {
                newWhere = newWhere.Where(a => a.customerName.Contains(input.Search.Keyword));
            }
            if (!input.Search.signhao.IsNullOrEmpty()) {
                newWhere = newWhere.Where(a=>a.BusinessOrderNo.Contains(input.Search.signhao));
            }

            if (!input.Search.Status.IsNullOrEmpty()) {
                newWhere = newWhere.Where(a => a.RechargeStatus == int.Parse(input.Search.Status));
            }
            if (!input.Search.serviceName.IsNullOrEmpty()) {
                newWhere = newWhere.Where(a => a.ServiceProviderName == input.Search.serviceName);
            }
             var Data = new 
            {
                Data = newWhere.Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows),
                Total = q.Count()
            };
            return Data;

        }


        public async Task<leasesystem_accountrecharge> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_accountrecharge data)
        {
            data.RechargeStatus = 1;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_accountrecharge data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<object> GetDataList(PageInput<ConditionDTO> input) 
        {
            var where = LinqHelper.True<leasesystem_accountrecharge>();
            var query = await Db.GetIQueryable<leasesystem_accountrecharge>()
                .Join(Db.GetIQueryable<leasesystem_merchant>(), a => a.MID, b => b.Id, (a, b) => new
                {
                    Id = a.Id,
                    BusinessOrderNo = a.BusinessOrderNo,
                    MID = a.MID,
                    customerName = b.customerName,
                    ServiceProviderName = a.ServiceProviderName,
                    ServiceAccountNo = a.ServiceAccountNo,
                    RechargeAmount = a.RechargeAmount,
                    ruleType = a.ruleType,
                    RechargeStatus = a.RechargeStatus
                }).ToListAsync();
            var result = new
            {
                Total = query.Count(),
                Data = query.Skip((input.PageIndex - 1) * input.PageRows)
            };
            return result;
        }

        public async Task<object> GetAccountInfo(PageInput<ConditionDTO> input,string id)
        {
            //根据用户id查询机构id
            var user =  Db.GetIQueryable<Base_User>().Where(a => a.Id == id).FirstOrDefault();
            if (user.brandId.IsNullOrEmpty()) {
                throw new System.NotImplementedException("该登陆不为客户无法查询账户信息");
            }
            //代发金额
            decimal? batchmoney =await Db.GetIQueryable<leasesystem_transactionbatch>().Where(a=>a.MID == user.brandId)
                .Join(Db.GetIQueryable<leasesystem_transactionbatchsub>(), a => a.Id, b => b.TBID, (a, b) =>
                new {
               b.trxSts,
               b.PaymentOutAmount
           }).Where(a => a.trxSts == "1" || a.trxSts == "2").SumAsync(a => a.PaymentOutAmount);
            //充值金额
            decimal? recharge =await  Db.GetIQueryable<leasesystem_accountrecharge>().Where(a => a.RechargeStatus == 1).SumAsync(a => a.RechargeAmount);

            var merchant = await  Db.GetIQueryable<leasesystem_merchant>().Where(a => a.Id == user.brandId).FirstAsync();

            var bankname = Db.GetIQueryable<leasesystem_bankcard>().Where(a=>a.MID == user.brandId).FirstOrDefault().AccountBank;

            var rechargeList = await  Db.GetIQueryable<leasesystem_accountrecharge>().ToListAsync();

            var newwhere = rechargeList.Where(a => a.CreateTime.ToString("yyyy-MM-dd") == input.Search.Time);

            var result = new { 
                Merchantaccount = recharge - batchmoney,
                BankName = bankname,
                MerchantName = merchant.customerName,
                RechargeList = newwhere.Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows),
                Total = rechargeList.Count()
            };
            return result;
        }

       
        #endregion

        #region 私有成员

        #endregion
    }
}